package com.ncmmall.mall.controller.business.subject;

import com.ncmmall.application.business.subject.SubjectActivityTypeApplication;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.subject.SubjectActivityType;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 专题类型管理
 *
 * @author likaikai
 * @date 2020-09-01 10:47:57
 */
@Controller
@RequestMapping("/mall/subjectActivityType")
@Slf4j
public class SubjectActivityTypeController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private SubjectActivityTypeApplication subjectActivityTypeApplication;

    /**
     * 专题类型列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageParams pageParams, Model model, SubjectActivityType params) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo()).setPageSize(pageParams.getPageSize());
        String hql = "select t from SubjectActivityType t where 1 = 1";
        Map<String, Object> map = new HashMap<>();
        if (params.getStatus() > 0) {
            hql += " and t.status = :status";
            map.put("status", params.getStatus());
        }
        if (StringUtils.isNotBlank(params.getName())) {
            hql += " and t.name like :name";
            map.put("name", "%" + params.getName() + "%");
        }
        hql += " order by t.createTime desc";
        QueryPage<SubjectActivityType> page = queryChannelService.findPage(hql, pageRequest.setParams(map), SubjectActivityType.class);
        model.addAttribute("page", page);
        model.addAttribute("params", params);
        return "mall/subjectActivity/type/list";
    }

    /**
     * 添加专题类型
     *
     * @param name 类型名称
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(String name) {
        try {
            Requires.hasLength(name, "专题类型名称不能为空");
            int count = queryChannelService.selectCount("select count(*) from mall_subject_activity_type where name = :name", ImmutableMap.of("name", name));
            Requires.isTrue(count == 0, "专题名称已存在");

            subjectActivityTypeApplication.create(new SubjectActivityType().setName(name));
            return ImmutableMap.of("status", true, "message", "添加成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[添加专题类型异常]", e);
            return ImmutableMap.of("status", false, "message", "添加异常");
        }
    }

    /**
     * 上架
     */
    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> enable(long id) {
        try {
            SubjectActivityType subjectActivityType = queryChannelService.findOne(SubjectActivityType.class, id);
            Requires.notNull(subjectActivityType, "专题类型不存在");

            subjectActivityTypeApplication.enable(subjectActivityType);
            return ImmutableMap.of("status", true, "message", "上架成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[专题类型上架异常]", e);
            return ImmutableMap.of("status", false, "message", "上架异常");
        }
    }

    /**
     * 下架
     */
    @RequestMapping(value = "/disable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> disable(long id) {
        try {
            SubjectActivityType subjectActivityType = queryChannelService.findOne(SubjectActivityType.class, id);
            Requires.notNull(subjectActivityType, "专题类型不存在");

            subjectActivityTypeApplication.disable(subjectActivityType);
            return ImmutableMap.of("status", true, "message", "下架成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[专题类型下架异常]", e);
            return ImmutableMap.of("status", false, "message", "下架异常");
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> remove(long id) {
        try {
            SubjectActivityType subjectActivityType = queryChannelService.findOne(SubjectActivityType.class, id);
            Requires.notNull(subjectActivityType, "专题类型不存在");

            int count = queryChannelService.selectCount("select count(*) from mall_subject_activity where type_id = :tid", ImmutableMap.of("tid", id));
            Requires.isTrue(count == 0, "专题类型正在被使用，无法删除");

            subjectActivityTypeApplication.remove(subjectActivityType);
            return ImmutableMap.of("status", true, "message", "删除成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[专题类型删除异常]", e);
            return ImmutableMap.of("status", false, "message", "删除异常");
        }
    }


}
